    package com.yyx.common.exception;

    import com.yyx.common.R;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;

    import java.sql.SQLIntegrityConstraintViolationException;

    @ControllerAdvice(annotations = {RestController.class, Controller.class})
    @ResponseBody
    @Slf4j
    public class GlobalExceptionHandler {

        @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
        public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
            log.error(ex.getMessage());

            if(ex.getMessage().contains("Duplicate entry")){
                String[] s = ex.getMessage().split(" ");
                return R.error(s[2]+"已存在");
            }
            return R.error("系统错误");
        }
//全局中捕获自定义异常  删除分类异常
        @ExceptionHandler(CustomException.class)
        public R<String> exceptionHandler(CustomException ex){
            log.error(ex.getMessage());
            return R.error(ex.getMessage());
        }
    }
